package com.wxl.common.util

import android.app.Activity
import android.content.Context
import android.graphics.Insets
import android.os.Build
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.util.DisplayMetrics
import android.view.View
import android.view.WindowInsets
import android.view.WindowMetrics
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.RelativeLayout


/**
 * create file time : 2021/3/2
 * create user : wxl
 * subscribe :
 */
class Screen {
    companion object {

        fun marginTopStatusBar(view: View) {
            marginTopView(getStatusBarHeight(view.context), view)
        }

        fun addStatusBarHeight(view: View){
            val statusBarHeight = getStatusBarHeight(view.context)
            view.layoutParams.height = view.layoutParams.height+ statusBarHeight
            view.requestLayout()
        }

        fun getStatusBarHeight(context: Context): Int {
            val resourceId: Int =
                context.resources.getIdentifier("status_bar_height", "dimen", "android")
            if(resourceId > 0) {
                return context.resources.getDimensionPixelSize(resourceId)
            } else {
                return Math.ceil(((if (VERSION.SDK_INT >= VERSION_CODES.M) 24 else 25) * context.resources.getDisplayMetrics().density).toDouble()).toInt()
            }
        }

        private fun marginTopView(margin: Int, view: View) {
            val params = view.layoutParams
            if (params is RelativeLayout.LayoutParams) {
                params.topMargin = margin
            }
            if (params is LinearLayout.LayoutParams) {
                params.topMargin = margin
            }
            if (params is FrameLayout.LayoutParams) {
                params.topMargin = margin
            }
        }

        fun getHeight(context: Context): Int {
            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                val windowMetrics: WindowMetrics =
                    (context as Activity).getWindowManager().getCurrentWindowMetrics()
                val insets: Insets = windowMetrics.windowInsets
                    .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
                windowMetrics.bounds.height() - insets.top - insets.bottom
            } else {
                val displayMetrics = DisplayMetrics()
                (context as Activity).getWindowManager().getDefaultDisplay()
                    .getMetrics(displayMetrics)
                displayMetrics.heightPixels
            }
        }

        fun getWidth(context: Context): Int {
            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                val windowMetrics: WindowMetrics =
                    (context as Activity).getWindowManager().getCurrentWindowMetrics()
                val insets: Insets = windowMetrics.windowInsets
                    .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
                windowMetrics.bounds.width() - insets.left - insets.right
            } else {
                val displayMetrics = DisplayMetrics()
                (context as Activity).getWindowManager().getDefaultDisplay()
                    .getMetrics(displayMetrics)
                displayMetrics.widthPixels
            }
        }
    }
}